package com.andaily.web.controller.mobile.thing;

import com.andaily.domain.dto.thing.ThingAmountDto;
import com.andaily.domain.dto.thing.ThingDto;
import com.andaily.domain.dto.thing.ThingHistoryDto;
import com.andaily.domain.dto.thing.ThingSearchDto;
import com.andaily.domain.thing.ThingPriority;
import com.andaily.domain.thing.ThingStatus;
import com.andaily.service.ThingService;
import com.andaily.web.controller.validator.ThingDtoValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * @author Shengzhao Li
 */
@Controller
@RequestMapping("/thing")
public class ThingController {

    @Autowired
    private ThingService thingService;

    private ThingDtoValidator thingDtoValidator = new ThingDtoValidator();

    @RequestMapping("overview")
    public String overview(Model model) throws Exception {
        ThingAmountDto thingAmountDto = thingService.loadThingAmountDto();
        model.addAttribute("thingAmountDto", thingAmountDto);
        return "thing/thing_overview";
    }


    @RequestMapping(value = "form", method = RequestMethod.GET)
    public String form(String guid, Model model) {
        ThingDto thingDto = thingService.loadThingDto(guid);
        model.addAttribute("thingDto", thingDto);
        return "thing/thing_form";
    }

    @RequestMapping(value = "form", method = RequestMethod.POST)
    public String submit(@ModelAttribute("thingDto") ThingDto thingDto, BindingResult result) {
        thingDtoValidator.validate(thingDto, result);
        if (result.hasErrors()) {
            return "thing/thing_form";
        }
        thingService.persistThingDto(thingDto);
        return "redirect:" + (thingDto.isAddNext() ? "form?added=true" : "overview?addThing=true");
    }


    @RequestMapping("history/overview")
    public String historyOverview(ThingHistoryDto thingHistoryDto, Model model) throws Exception {
        thingHistoryDto = thingService.loadThingHistoryDto(thingHistoryDto);
        model.addAttribute("thingHistoryDto", thingHistoryDto);
        return "thing/thing_history_overview";
    }

    @RequestMapping("history/overview/more")
    public String historyOverviewMore(ThingHistoryDto thingHistoryDto, Model model) throws Exception {
        thingHistoryDto = thingService.loadThingHistoryDto(thingHistoryDto);
        model.addAttribute("thingHistoryDto", thingHistoryDto);
        return "thing/thing_history_overview_more";
    }

    @RequestMapping("history/details/{guid}")
    public String historyDetails(@PathVariable() String guid, Model model) throws Exception {
        ThingDto thingDto = thingService.loadExistThingDto(guid);
        model.addAttribute("thingDto", thingDto);
        return "thing/thing_history_details";
    }

    @RequestMapping("search/initial")
    public String initialSearch(String key, Model model) {
        model.addAttribute("key", key);
        return "thing/thing_search_page";
    }

    @RequestMapping("search/load")
    public String loadSearchResult(ThingSearchDto thingSearchDto, Model model) {
        thingSearchDto = thingService.loadSearchResult(thingSearchDto);
        model.addAttribute("thingSearchDto", thingSearchDto);
        return "thing/thing_search_result";
    }

    @RequestMapping("search/load/more")
    public String moreSearchResult(ThingSearchDto thingSearchDto, Model model) {
        thingSearchDto = thingService.loadSearchResult(thingSearchDto);
        model.addAttribute("thingSearchDto", thingSearchDto);
        return "thing/thing_search_result_more";
    }

    @RequestMapping("search/action/{guid}")
    public String actionThing(@PathVariable() String guid, String key, Model model) {
        ThingDto thingDto = thingService.loadExistThingDto(guid);
        model.addAttribute("thingDto", thingDto);
        model.addAttribute("hasHighThings", hasHighThings(thingDto));
        model.addAttribute("key", key);
        return "thing/thing_search_result_action";
    }

    private boolean hasHighThings(ThingDto thingDto) {
        boolean hasHighThings = thingService.hasHighThings();
        return hasHighThings
                && ThingStatus.CREATED.equals(thingDto.getStatus())
                && !(ThingPriority.HIGH.equals(thingDto.getPriority()) || ThingPriority.HIGHER.equals(thingDto.getPriority()));
    }

    @RequestMapping("search/changestatus/{guid}/{status}")
    public String changeStatus(@PathVariable("guid") String guid, @PathVariable("status") ThingStatus status, String key) {
        thingService.changeThingStatus(guid, status);
        return "redirect:../../load?result=true&key=" + key;
    }

}
